package com.example.javamaday.leetcode;

public class LeetCodeTest0035 {

    public static void main(String[] args) {
        int[] nums = new int[]{1};
        System.out.println(searchInsert(nums, 3));
    }

    public static int searchInsert(int[] nums, int target) {
        int min = 0;
        int max = nums.length;
        int index = 0;
        while (min < max) {
            index = (min + max) / 2;
            if (nums[index] > target) {
                if (max == index) {
                    if (nums[max] > target) {
                        return max;
                    } else {
                        return max + 1;
                    }
                }
                max = index;
            } else if (nums[index] == target) {
                return index;
            } else {
                if (min == index) {
                    if (nums[min] < target) {
                        return min + 1;
                    } else {
                        return min;
                    }
                }
                min = index;
            }
        }
        return index;
    }

}
